package com.tencent.yolov5ncnn

import android.content.Context
import android.net.wifi.WifiConfiguration
import android.net.wifi.WifiInfo
import android.net.wifi.WifiManager
import android.util.Log

class WifiUtils(context: Context) {
    private val wifiManager =
        context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager

    val isWifiEnabled: Boolean
        // 检查WiFi是否可用
        get() = wifiManager.isWifiEnabled

    // 打开WiFi
    fun openWifi() {
        if (!wifiManager.isWifiEnabled) {
            wifiManager.setWifiEnabled(true)
        }
    }

    // 关闭WiFi
    fun closeWifi() {
        if (wifiManager.isWifiEnabled) {
            wifiManager.setWifiEnabled(false)
        }
    }

    // 扫描WiFi网络
    fun scanWifi() {
        wifiManager.startScan()
    }

    val scanResults: List<WifiConfiguration>
        // 获取扫描到的WiFi列表
        get() = wifiManager.configuredNetworks

    val connectedWifiInfo: WifiInfo
        // 获取当前连接的WiFi
        get() = wifiManager.connectionInfo

    val isWifiConnected: Boolean
        // 判断是否有当前可用的WiFi连接
        get() {
            val wifiInfo = wifiManager.connectionInfo
            return wifiInfo != null && wifiInfo.networkId != -1
        }

    // 关闭所有连接
    fun disconnectWifi() {
        wifiManager.disconnect()
    }

    // 连接WiFi
    fun connectToWifi(wifiConfig: WifiConfiguration?) {
        val netId = wifiManager.addNetwork(wifiConfig)
        wifiManager.enableNetwork(netId, true)
        Log.d("opop","$netId")
    }

    // 移除WiFi
    fun removeWifi(netId: Int) {
        wifiManager.removeNetwork(netId)
        wifiManager.saveConfiguration()
    }

    // 添加WiFi到系统
    fun addNetwork(wifiConfig: WifiConfiguration?): Int {
        return wifiManager.addNetwork(wifiConfig)
    }

    // 创建配置
    fun createWifiConfig(ssid: String, password: String, type: Int): WifiConfiguration {
        val config = WifiConfiguration()
        config.SSID = "\"" + ssid + "\""
        when (type) {
            1 -> {
                config.hiddenSSID = true
                config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE)
            }

            2 -> {
                config.hiddenSSID = true
                config.wepKeys[0] = "\"" + password + "\""
                config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN)
                config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED)
                config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE)
                config.wepTxKeyIndex = 0
            }

            3 -> config.preSharedKey = "\"" + password + "\""
        }
        return config
    }

    // 获取是否已经存在的配置
    fun isExist(ssid: String): WifiConfiguration? {
        val existingConfigs = wifiManager.configuredNetworks
        for (existingConfig in existingConfigs) {
            if (existingConfig.SSID == "\"" + ssid + "\"") {
                return existingConfig
            }
        }
        return null
    }

    // 去除同名WIFI
    fun removeSameWifi(ssid: String) {
        val existingConfigs = wifiManager.configuredNetworks
        for (existingConfig in existingConfigs) {
            if (existingConfig.SSID == "\"" + ssid + "\"") {
                wifiManager.removeNetwork(existingConfig.networkId)
            }
        }
    }

    // 判断一个扫描结果中，是否包含了某个名称的WIFI
    fun isScanResultExist(ssid: String, existingConfigs: List<WifiConfiguration>): Boolean {
        for (existingConfig in existingConfigs) {
            if (existingConfig.SSID == "\"" + ssid + "\"") {
                return true
            }
        }
        return false
    }

    // WiFi安全类型枚举
    enum class WifiSecurityType {
        OPEN, WEP, WPA
    }
}


